package com.sxfq.loansupermarket.backsystem.modular.logsf.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.sxfq.loansupermarket.backsystem.common.constant.factory.PageFactory;
import com.sxfq.loansupermarket.backsystem.common.controller.BaseController;
import com.sxfq.loansupermarket.backsystem.core.log.LogManager;
import com.sxfq.loansupermarket.backsystem.core.log.factory.LogTaskFactory;
import com.sxfq.loansupermarket.backsystem.core.shiro.ShiroKit;
import com.sxfq.loansupermarket.backsystem.core.support.HttpKit;
import com.sxfq.loansupermarket.backsystem.modular.arrival.service.IReturnVisitService;
import com.sxfq.loansupermarket.backsystem.modular.logsf.entity.ApprovalProcessAuditInfo;
import com.sxfq.loansupermarket.backsystem.modular.logsf.entity.SearchApprovalProcessAuditInfo;
import com.sxfq.loansupermarket.backsystem.modular.logsf.service.IApprovalProcessService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * 渠道审核日志
 *
 * @author
 * @Date
 */
@Controller
@RequestMapping("/approvalProcessLog")
public class ApprovalProcessAuditController extends BaseController {
    @Autowired
    IReturnVisitService service;

    @Autowired
    IApprovalProcessService approvalProcessService;

    private String PREFIX = "/returnVisit/";

    /**
     * 跳转到审核日志界面
     */
    @RequestMapping("/audit")
    public String audit(Model model) {
        String tableHead = ShiroKit.getUser().getAbbreviation();
        if (tableHead == "" || tableHead == null) {
            LogManager.me().executeLog(LogTaskFactory.exitLog(ShiroKit.getUser().getId(), HttpKit.getIp()));
            ShiroKit.getSubject().logout();
            return "/login.html";
        }
        model.addAttribute("outpatientManagementList", ShiroKit.getSessionAttr("outpatientManagementList"));
        return PREFIX + "approval_process_audit.html";
    }

    /**
     * 审核日志管理列表
     */
    @RequestMapping("/list")
    @ResponseBody
    public Object ApprovalProcessAuditList(SearchApprovalProcessAuditInfo info, HttpServletRequest request) {
        Page<ApprovalProcessAuditInfo> page = new PageFactory<ApprovalProcessAuditInfo>().defaultPage();
        String tableHead = ShiroKit.getUser().getAbbreviation();
        if (tableHead == "" || tableHead == null) {
            LogManager.me().executeLog(LogTaskFactory.exitLog(ShiroKit.getUser().getId(), HttpKit.getIp()));
            ShiroKit.getSubject().logout();
            return "/login.html";
        }
        try {
            List<ApprovalProcessAuditInfo> list = new ArrayList<ApprovalProcessAuditInfo>();
            list = approvalProcessService.getList(page, info);
            if (list.size() == 0) {
                page.setCurrent(1);
                list = approvalProcessService.getList(page, info);
            }
            page.setRecords(list);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return super.packForBT(page);
    }
}